import { formatDateTime } from '@fl/utils/fn.ts'
import { cloneDeep } from 'lodash-es'
import { reactive } from 'vue'

export function useTable() {
    const gridOptions = reactive({
        border: true,
        columnConfig: {
            resizable: true,
        },
        toolbarConfig: {
          fixed: true
        },
        columns: [
            { type: 'index', slots: { default: 'index' }, width: 60, title: '序号', },
            { field: 'blockNumber', title: '板块编号', minWidth: 80 },
            { field: 'areaCode', title: '所属区域', minWidth: 80, slots: { default: 'regionName' }},
            { field: 'company', title: '竞得公司', minWidth: 100 },
            { field: 'landLocation', title: '土地位置', minWidth: 80 },
            { field: 'categoryName', slots: { default: 'categoryName' }, title: '土地用途', minWidth: 100 },
            { field: 'landArea', title: '土地面积(㎡)', minWidth: 80 },
            { field: 'transactionDate', title: '成交时间', minWidth: 100 },
            { fixed: 'right', slots: { default: 'operate' }, title: '操作', width:150 },
        ],
        data: [],
        editConfig: {
            mode: 'row',
            showStatus: true,
            trigger: 'manual',
        },
        height: 594,
        keepSource: true,
        loading: false,
        pagerConfig: {
            currentPage: 1,
            enabled: true,
            pageSize: 10,
            pageSizes: [10, 20, 50, 100, 200, 500],
            total: 0,
        },
        showOverflow: true,
        stripe: true,
    })

    const btns = [
        {
            label: '编辑',
            text: true,
            value: 'edit',
        },
        {
            label: '删除',
            text: true,
            value: 'delete',
        }
    ]

    const addBtn = {
        label: '新建',
        text: true,
        value: 'add',
    }

    // 删除空属性
    const paramsIsNull = (obj) => {
        const _newPar = {}
        for (const key in obj) {
            if (
                (obj[key] === 0 || obj[key])
                && obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== ''
            ) {
                _newPar[key] = obj[key]
            }
        }
        // 返回新对象
        return _newPar
    }

    // TODO:多条件查询方法 仅字符串对比 模糊匹配
    const useQuery = (data, params) => {
        const notNullParams = paramsIsNull(params)
        return data.filter((item) => {
            return Object.keys(notNullParams).every((key: string) => {
                if (key === 'createTime') {
                    const target = item[key] ? item[key].replace('T', ' ') : ''
                    const [start, end] = notNullParams[key]

                    console.log('start--', formatDateTime(start))
                    console.log('end--', formatDateTime(end))
                    console.log('target--', target)

                    const t = new Date(target).getTime()
                    const s = new Date(start).getTime()
                    const e = new Date(end).getTime()
                    return (
                        s <= t && e >= t
                    )
                }
                else {
                    const flag = item[key] ? item[key].indexOf(notNullParams[key]) !== -1 : false
                    return (
                        item[key] !== undefined
                        && flag
                    )
                }
            })
        })
    }

    // 前端分页
    const usePage = (data, gridOptions) => {
        const { currentPage, pageSize } = gridOptions.pagerConfig
        const val = cloneDeep(data)
        const list = val.splice((currentPage - 1) * pageSize, pageSize)
        return {
            list,
            total: data.length,
        }
    }

    return { addBtn, btns, gridOptions, usePage, useQuery }
}
